﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================

using System.Collections.Generic;

namespace XCI.Core
{
    /// <summary>
    /// 表示一个键值对
    /// </summary>
    public class KeyValue
    {
        /// <summary>
        /// 初始化键值对
        /// </summary>
        public KeyValue()
        {
        }

        /// <summary>
        /// 指定键初始化键值对，键和值相同
        /// </summary>
        /// <param name="key">指定的键，键和值相同</param>
        public KeyValue(string key)
            : this(key, key)
        {
        }

        /// <summary>
        /// 指定键值初始化键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public KeyValue(string key, string value)
        {
            this.Key = key;
            this.Value = value;
        }

        /// <summary>
        /// 键
        /// </summary>
        public string Key { get; set; }

        /// <summary>
        /// 值
        /// </summary>
        public string Value { get; set; }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != this.GetType()) return false;
            return Key == ((KeyValue)obj).Key;
        }

        public override int GetHashCode()
        {
            if (Key == null) return 0;
            return Key.GetHashCode();
        }

        public static bool operator ==(KeyValue left, KeyValue right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(KeyValue left, KeyValue right)
        {
            return !Equals(left, right);
        }

        public override string ToString()
        {
            return $"{nameof(Key)}: {Key}, {nameof(Value)}: {Value}";
        }
    }

    public class KeyValue<TKey, TValue>
    {
        /// <summary>
        /// 初始化键值对
        /// </summary>
        public KeyValue()
        {
        }

        /// <summary>
        /// 指定键值初始化键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public KeyValue(TKey key, TValue value)
        {
            this.Key = key;
            this.Value = value;
        }

        /// <summary>
        /// 键
        /// </summary>
        public TKey Key { get; set; }

        /// <summary>
        /// 值
        /// </summary>
        public TValue Value { get; set; }
    }

    public class KeyValueSet : HashSet<KeyValue>
    {

    }

    public class KeyValueList : List<KeyValue>
    {

    }

    public class IdName
    {
        /// <summary>
        /// 构建IdName对象
        /// </summary>
        public IdName()
        {
        }

        /// <summary>
        /// 构建IdName对象
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="name">名称</param>
        public IdName(string id, string name)
        {
            Id = id;
            Name = name;
        }

        /// <summary>
        /// 值
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
    }

    /// <summary>
    /// 表示一个键值简单对象
    /// </summary>
    public class Simple
    {
        /// <summary>
        /// 构建键值简单对象
        /// </summary>
        public Simple()
        {
        }

        /// <summary>
        /// 构建键值简单对象
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="name">名称</param>
        public Simple(string id, string name)
        {
            Id = id;
            Name = name;
        }

        /// <summary>
        /// 构建键值简单对象
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="name">名称</param>
        /// <param name="spell">简拼</param>
        public Simple(string id, string name, string spell)
        {
            Id = id;
            Name = name;
            Spell = spell;
        }

        /// <summary>
        /// 构建键值简单对象
        /// </summary>
        /// <param name="id">Id</param>
        /// <param name="name">名称</param>
        /// <param name="spell">简拼</param>
        /// <param name="code">编码</param>
        public Simple(string id, string name, string spell, string code)
        {
            Id = id;
            Name = name;
            Spell = spell;
            Code = code;
        }

        /// <summary>
        /// 值
        /// </summary>
        public string Id { get; set; }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 简拼
        /// </summary>
        public string Spell { get; set; }

        /// <summary>
        /// 编码
        /// </summary>
        public string Code { get; set; }
    }

    /// <summary>
    /// 表示一个名称描述字段
    /// </summary>
    public class NameCaption
    {
        /// <summary>
        /// 初始化名称描述
        /// </summary>
        public NameCaption()
        {
        }

        /// <summary>
        /// 初始化名称描述
        /// </summary>
        /// <param name="name">名称</param>
        public NameCaption(string name) : this(name, name)
        {
        }

        /// <summary>
        /// 初始化名称描述
        /// </summary>
        /// <param name="name">名称</param>
        /// <param name="caption">描述</param>
        public NameCaption(string name, string caption)
        {
            this.Name = name;
            this.Caption = caption;
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 描述
        /// </summary>
        public string Caption { get; set; }
    }

    /// <summary>
    /// 表示一个键值对以及描述字段
    /// </summary>
    public class KeyValueCaption : KeyValue
    {
        /// <summary>
        /// 初始化键值对
        /// </summary>
        public KeyValueCaption()
        {
        }

        /// <summary>
        /// 指定键值初始化键值对
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public KeyValueCaption(string key, string value)
            : base(key, value)
        {
        }

        /// <summary>
        /// 指定键值初始化键值对以及描述字段
        /// </summary>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <param name="caption">描述</param>
        public KeyValueCaption(string key, string value, string caption)
            : base(key, value)
        {
            this.Caption = caption;
        }

        /// <summary>
        /// 描述
        /// </summary>
        public string Caption { get; set; }
    }

    /// <summary>
    /// 表示一个主标识键值对，除了包含键和值之外，还包含一个唯一标识
    /// </summary>
    public class PrimaryKeyValue : KeyValue
    {
        /// <summary>
        /// 初始化主标识键值对
        /// </summary>
        public PrimaryKeyValue()
        {
        }

        /// <summary>
        /// 指定唯一标识和键值初始化主标识键值对
        /// </summary>
        /// <param name="id">唯一标识</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public PrimaryKeyValue(string id, string key, string value)
            : base(key, value)
        {
            Id = id;
        }

        /// <summary>
        /// 唯一标识
        /// </summary>
        public string Id { get; set; }

        public override string ToString()
        {
            return $"Id={Id};Key={Key};Value={Value}";
        }
    }

    /// <summary>
    /// 表示一个上下级键值对，除了包含键和值之外，还包含一个唯一标识
    /// </summary>
    public class ParentKeyValue : PrimaryKeyValue
    {
        /// <summary>
        /// 初始化上下级键值对
        /// </summary>
        public ParentKeyValue()
        {
        }

        /// <summary>
        /// 指定上级唯一标识、唯一标识、键值初始化上下级键值对
        /// </summary>
        /// <param name="id">唯一标识</param>
        /// <param name="parentId">上级唯一标识</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        public ParentKeyValue(string id, string parentId, string key, string value)
            : base(id, key, value)
        {
            ParentId = parentId;
        }

        /// <summary>
        /// 上级唯一标识
        /// </summary>
        public string ParentId { get; set; }

        public override string ToString()
        {
            return $"Id={Id};ParentId={ParentId};Key={Key};Value={Value}";
        }
    }
}